package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.MessageHistoryDto;

/**
 * @author xwei
 *
 */
public class TmsMessageHistoryDaoImp extends TmsDao implements TmsMessageHistoryDao{

	protected TmsMessageHistoryDaoImp() throws TmsDaoException {
		super();
	}

	@Override
	public void insert(MessageHistoryDto dto) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;	
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO  ");
			sql.append(TmsTableNames.TMS_MESSAGE_HISTORY);
			sql.append(" (  ");
			sql.append(" PAYLOADCLASS,  ");
			sql.append(" PAYLOADDATA,  ");
			sql.append(" PROTOCOL,  ");
			sql.append(" TYPE,  ");
			sql.append(" RESULT,  ");
			sql.append(" RESULTMESSAGE,  ");
			sql.append(" ACTION,  ");
			sql.append(" MESSAGEID,  ");
			sql.append(" UPDATEUSERID,  ");
			sql.append(" UPDATETIME  ");
			sql.append(" )  ");
			sql.append(" VALUES  ");
			sql.append(" (?,?,?,?,?,?,?,?,?,SYSDATE()) ");
			ps = conn.prepareStatement(sql.toString());
			
			ps.setString(1, dto.getPayloadClass());
			ps.setBytes(2, dto.getPayloadData());
			ps.setInt(3, dto.getProtocol());
			ps.setInt(4, dto.getType());
			ps.setInt(5, dto.getResult());
			ps.setString(6, dto.getResultMessage());
			ps.setInt(7, dto.getAction());
			ps.setString(8, dto.getMessageId());
			ps.setInt(9, dto.getUpdateUserId());
			ps.executeUpdate();	
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			super.close(rs, ps);
		}						
	}
}
